package run.halo.app.model.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.*;

/**
 * Classify entity.
 *
 * @author meazty
 * @date 2022-05-12
 */
@Data
@Entity
@Table(name = "classify", indexes = {
    @Index(name = "classify_name", columnList = "name"),
    @Index(name = "classify_parent_id", columnList = "parent_id")})
@ToString
@EqualsAndHashCode(callSuper = true)
public class Classify extends BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY, generator = "custom-id")
    @GenericGenerator(name = "custom-id",
        strategy = "run.halo.app.model.entity.support.CustomIdGenerator")
    private Integer id;

    /**
     * Classify name.
     */
    @Column(name = "name", nullable = false)
    private String name;

    /**
     * Classify code.
     */
    @Column(name = "code")
    private String code;

    /**
     * alias 别名
     */
    @Column(name = "alias")
    private String alias;

    /**
     * Classify icon.
     */
    @Column(name = "icon")
    private String icon;

    /**
     * Classify cover.
     */
    @Column(name = "cover")
    private String cover;

    /**
     * Classify intro.
     */
    @Column(name = "intro", length = 500)
    private String intro;

    /**
     * Parent Classify.
     */
    @Column(name = "parent_id")
    @ColumnDefault("0")
    private Integer parentId;

    /**
     * Priority Classify.
     */
    @Column(name = "priority")
    @ColumnDefault("0")
    private Integer priority;

    /**
     * Classify password.
     */
    @Column(name = "password")
    private String password;

    @Override
    public void prePersist() {
        super.prePersist();

        if (parentId == null || parentId < 0) {
            parentId = 0;
        }

        if (priority == null) {
            priority = 0;
        }
    }

}
